package com.liveneo.stoat.common.toolbox.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FilenameUtils;
/**
 * 
 *@Description: 获取图片真实后缀  png/jpg
 *@Author:hjt 
 *@Since:2017年2月9日 上午11:45:13 
 *@Version:1.0.0
 */
public class FileExt {
	private static final String JPG_HEX = "ff";
	private static final String PNG_HEX = "89";
	private static final String JPG_EXT = "jpg";
	private static final String PNG_EXT = "png";
	public static String getFileExt(String filePath) {
	    FileInputStream fis = null;
	    String extension = FilenameUtils.getExtension(filePath);
	    try {
	        fis = new FileInputStream(new File(filePath));
	        byte[] bs = new byte[1];
	        fis.read(bs);
	        String type = Integer.toHexString(bs[0]&0xFF);
	        if(JPG_HEX.equals(type))
	            extension = JPG_EXT;
	        if(PNG_HEX.equals(type))
	            extension = PNG_EXT;
	    } catch (FileNotFoundException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            if(fis != null)
	                fis.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	    return extension;
	}
}
